package com.game77.guessTheWords2.layer;

import java.util.HashMap;

import android.graphics.Typeface;

import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.entity.StageData;
import com.game77.guessTheWords2.scene.ClassChooseScene;
import com.game77.guessTheWords2.scene.MainScene;
import com.game77.guessTheWords2.sprite.StageBlock;
import com.game77.guessTheWords2.util.AudioHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.NinePatchSprite;
import com.wiyun.engine.nodes.ScrollableLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

public class StageLayer extends Layer {

	Sprite background;

	Button btnReturn;
	Label stageTip;
	Label currentClass;
	ScrollableLayer layer;

	HashMap<Integer, StageBlock> stages = new HashMap<Integer, StageBlock>();

	public StageLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_stage_back);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		background.setScaleX(GameSystem.Screen_Width
				/ GameSystem.tex_game_background.getWidth());
		background.setScaleY(GameSystem.Screen_Height
				/ GameSystem.tex_game_background.getHeight());
		this.addChild(background);

		loadStages();

		// return button
		btnReturn = Button.make(R.drawable.btn_back, R.drawable.btn_back_press,
				this, "onReturnClick");
		btnReturn.setAnchor(0.5f, 0.5f);
		btnReturn.setPosition(25, 775);
		addChild(btnReturn);

		stageTip = Label.make("点击GO开始游戏吧！", 18, Typeface.BOLD);
		stageTip.setColor(WYColor3B.make(255, 255, 0));
		stageTip.setAnchor(1, 0);
		stageTip.setPosition(460, 2);
		addChild(stageTip);

		currentClass = Label.make("1", 50, Typeface.BOLD);
		currentClass.setColor(WYColor3B.make(230, 230, 230));
		currentClass.setPosition(240, 775);
		addChild(currentClass);

	}

	private void loadStages() {
		layer = ScrollableLayer.make();
		layer.setContentSize(GameSystem.Screen_Width,
				GameSystem.Screen_Height - 100);
		layer.setRelativeAnchorPoint(true);
		layer.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 - 20);
		layer.setVertical(true);
		layer.setLeftMargin((GameSystem.Screen_Width - GameSystem.tex_stage_block
				.getWidth()) / 2);
		layer.setRightMargin((GameSystem.Screen_Width - GameSystem.tex_stage_block
				.getWidth()) / 2);
		layer.setTopMargin(50);
		layer.setBottomMargin(55);
		addChild(layer);

		// create thumb bar
		NinePatchSprite vThumb = NinePatchSprite.make(
				Texture2D.makePNG(R.drawable.vertical_thumb),
				WYRect.make(DP(5), DP(7), DP(1), DP(1)));

		// set thumb bar
		layer.setVerticalThumb(vThumb);
		layer.setThumbFadeOutTime(2.f);

		for (int i = 0; i < GameSystem.stageCount; i++) {
			int stageIndex = GameSystem.stageCount - i;
			StageBlock stageBlock = new StageBlock(stageIndex);
			stageBlock.setPosition(0, 90 * i);
			layer.addScrollableChild(stageBlock);
			stages.put(stageIndex, stageBlock);
		}
	}

	public void refreshStages() {
		currentClass.setText(StageManager.instance.getLastClass() + "");
		for (int i = 1; i <= GameSystem.stageCount; i++) {
			String stageKey = StageManager.instance.getLastClass() + "_" + i;
			if (StageManager.instance.getStageDatas().containsKey(stageKey)) {
				StageData sd = StageManager.instance.getStageDatas().get(
						stageKey);
				if (sd.getAllCount() > 0) {
					stages.get(i).setLockStatus(false);
					// 更新关卡完成情况
					stages.get(i).refreshStageInfo();
				} else {
					stages.get(i).setLockStatus(true);
				}
			} else {
				stages.get(i).setLockStatus(true);
			}

		}
	}

	public void scollToIndex(int index) {
		if (StageManager.instance.getLastStage() > 0) {
			layer.setOffsetY((index - 1) * 90);
		}
	}

	private float DP(float v) {
		return ResolutionIndependent.resolveDp(v);
	}

	public void onReturnClick() {
		AudioHelper.clickButton();
		GameUtil.switchSceneNormalFast(ClassChooseScene.make());
		ClassChooseScene.make().refreshClassInfos();
		ClassChooseScene.make().updateCoins();
	}
}
